package com.mall.product.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.common.utils.PageUtils;
import com.mall.common.utils.Query;
import com.mall.product.dao.BrandDao;
import com.mall.product.dao.CategoryBrandRelationDao;
import com.mall.product.dao.CategoryDao;
import com.mall.product.entity.BrandEntity;
import com.mall.product.entity.CategoryBrandRelationEntity;
import com.mall.product.entity.CategoryEntity;
import com.mall.product.service.CategoryBrandRelationService;
import com.mall.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {


    @Autowired
    private BrandDao brandDao;

    @Autowired
    private CategoryDao categoryDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<CategoryBrandRelationEntity> queryWrapper = new QueryWrapper<>();

        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),queryWrapper);

        return new PageUtils(page);
    }

    @Override
    public List<CategoryBrandRelationEntity> catelogList(Long data) {

        List<CategoryBrandRelationEntity> brandRelationEntities = this.list(new LambdaQueryWrapper<CategoryBrandRelationEntity>().eq(CategoryBrandRelationEntity::getBrandId, data));

        return brandRelationEntities;
    }


    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {

        Long brandId = categoryBrandRelation.getBrandId();

        Long catelogId = categoryBrandRelation.getCatelogId();

        // 1. 查询详细的名字
        BrandEntity brandEntity = brandDao.selectById(brandId);
        CategoryEntity categoryEntity = categoryDao.selectById(catelogId);

        categoryBrandRelation.setBrandName(brandEntity.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());

        this.save(categoryBrandRelation);

    }

    @Override
    public boolean updateBrand(Long brandId, String name) {

        CategoryBrandRelationEntity categoryBrandRelationEntity = new CategoryBrandRelationEntity();
        categoryBrandRelationEntity.setBrandId(brandId);
        categoryBrandRelationEntity.setBrandName(name);

        boolean update = this.update(categoryBrandRelationEntity, new LambdaUpdateWrapper<CategoryBrandRelationEntity>().eq(CategoryBrandRelationEntity::getBrandId, brandId));

        return update;
    }

    /**
     * 级联更新所有的关联数据
     *
     * @param categoryBrandRelation
     */
    @Override
    public void updateCascade(CategoryBrandRelationEntity categoryBrandRelation) {
        boolean b = this.updateById(categoryBrandRelation);

    }

    @Override
    public void updateCategory(Long catId, String name) {

        this.baseMapper.updateCategory(catId, name);
    }

    @Override
    public List<BrandVo> getBrandsByCatId(Long catId) {

        List<CategoryBrandRelationEntity>  data =  this.baseMapper.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id",catId));

        if(!CollectionUtils.isEmpty(data)){
            List<BrandVo> list = data.stream().map(e -> {
                BrandVo brandVo = new BrandVo();
                brandVo.setBrandId(e.getBrandId());
                brandVo.setBrandName(e.getBrandName());
                return brandVo;
            }).collect(Collectors.toList());

            return list;
        }

        return new ArrayList<>();
    }

}